﻿using UnityEngine;
using System.Collections;
using System;

public class DragonColler : MonoBehaviour
{

    public float timer = 0;
    //显示一秒15帧
    public int frameNumber = 15;
    //显示当前第0帧
    public int frameCount = 0;
    //飞龙的刚体组件引用
    private Rigidbody r;
    //让飞龙飞的标志位
    private bool IsFly = false;
    //使用委托完成加分数的事件
    public Action AddScore;
    public Action DragonDie;


    void Start()
    {
        r = GetComponent<Rigidbody>();
        
    }


    void Update()
    {
        if (IsFly)
        {
            
            timer += Time.deltaTime;//计数器随着时间增长而增长
            if (timer >= 1f / frameNumber)//当计数器大于一帧的时间
            {
                frameCount++;//帧率增加
                timer -= 1f / frameNumber;//计数器等于时间
                //再计算需要显示几帧
                int frameIndex = frameCount % 3;

                GetComponent<Renderer>().material.SetTextureOffset("_MainTex", new Vector2(0.33333f * frameIndex, 0));

            }
            if (Input.GetMouseButton(0))
            {
                r.velocity = new Vector3(0, 5f, 0);
            }
        }
        
    }

    //碰到柱子或者中间增加分数
    void OnTriggerEnter(Collider col)
    {
        if (col.transform.tag == "HG"||col.transform.tag == "Back")
        {
            if (DragonDie == null)
            {
                return;
            }
            DragonDie();
            
        }
        if (col.transform.tag == "Score")
        {
            if (AddScore == null)
            { 
                return;
            }
            AddScore();
        }
    }

    public void IsDragonFly(bool IsOn)
    {
        IsFly = IsOn;
        if (IsOn == true)
        {
            r.isKinematic = false;
        }
        else
            r.isKinematic = true;
    }
}
